﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;

namespace iPdf
{
    class ImageToPdfConverter
    {
        public static void ImageToPdfPage(Document doc, PdfWriter writer, string imageFileName)
        {
            if (TiffToPdfConverter.IsTiffFile(imageFileName))
            {
                TiffToPdfConverter.ImageToPdfPage(doc, writer, imageFileName);
                return;
            }

            MemoryStream ms = GetImage(imageFileName);
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ms.ToArray());
            img.SetAbsolutePosition(0, 0);
            doc.SetPageSize(new iTextSharp.text.Rectangle(img.Width, img.Height));
            doc.NewPage();
            PdfContentByte cb = writer.DirectContent;
            cb.AddImage(img);
        }

        public static MemoryStream GetImage(string imageFileName)
        {
            MemoryStream ms = new MemoryStream();
            System.Drawing.Image.FromFile(imageFileName).Save(ms, ImageFormat.Png);
            return ms;
        }
  
    }
}
